package com.example.jpetstoreclient.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jpetstoreclient.common.CommonResponse;
import com.example.jpetstoreclient.domain.Animal;
import com.example.jpetstoreclient.persistance.AnimalMapper;
import com.example.jpetstoreclient.service.AnimalService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("animalService")
public class AnimalServiceImpl extends ServiceImpl<AnimalMapper, Animal> implements AnimalService {


    @Override
    public CommonResponse<List<Animal>> getAllAnimals(){
        List<Animal> animals = list();
        return CommonResponse.createForSuccess("查询所有商品",animals);
    }

    @Override
    public CommonResponse<Animal> getAnimalById(int id) {
        Animal animal = getById(id);
        return CommonResponse.createForSuccess("按id查询商品",animal);
    }

    @Override
    public CommonResponse<List<Animal>> getAnimalsByName(String name) {
        QueryWrapper<Animal> wrapper = new QueryWrapper<>();
        wrapper.like("name",name);
        List<Animal> animals = list(wrapper);
        return CommonResponse.createForSuccess("按名查询商品",animals);
    }

    @Override
    public CommonResponse<List<Animal>> getAnimalsByType(String type) {
        QueryWrapper<Animal> wrapper = new QueryWrapper<>();
        wrapper.like("type",type);
        List<Animal> animals = list(wrapper);
        return CommonResponse.createForSuccess("按种类查询商品",animals);
    }

    @Override
    public CommonResponse<List<Animal>> getAnimalsByGood_Specific(String good_specific) {
        QueryWrapper<Animal> wrapper = new QueryWrapper<>();
        wrapper.like("good_specific",good_specific);
        List<Animal> animals = list(wrapper);
        return CommonResponse.createForSuccess("按具体种类查询商品",animals);
    }

    @Override
    public CommonResponse<List<String>> getAllTypes(){
        QueryWrapper<Animal> wrapper = new QueryWrapper<>();
        wrapper.select("type");
        List<Animal> animals = list(wrapper);
        List<String> types = new ArrayList<>();
        for(Animal animal : animals){
            String type = animal.getType();
            if(types.contains(type))continue;
            else types.add(animal.getType());
        }
        return CommonResponse.createForSuccess("查询所有类型",types);
    }
}
